package com.david;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@WebFilter(urlPatterns = "/**")
@Component
public class AuthFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("auth启动成功");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest)servletRequest;
        String token = req.getHeader("token");

        if (StringUtils.isEmpty(token)){
            System.err.println("你没登录");
        }else {
            String parseToken = JwtUtil.parseToken(token);
            if (!StringUtils.isEmpty(parseToken)){
                System.out.println("登录成功");
                //把往后面传递
                filterChain.doFilter(servletRequest,servletResponse);
            }
        }

        System.out.println("xxoo 来了老弟");

    }

    @Override
    public void destroy() {

    }
}
